/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

/**
 * Tests the functionality of the Script object.
 * @author J Patrick Davenport
 *
 */
public class ScriptTests extends ChildbearingPageElementTests {

	@Test
	public void testAddScriptContent() {
		final Script script = this.getScript();
		final String body = "function myLeftFoot() {\r\n" +
								"var wittyStatement = \"Goes here\";\r\n" +
							"}\r\n";
		script.setScriptBody(body);
		final String scriptBody = script.getScriptBody();
		assertEquals(body, scriptBody);
	}

	@Test
	public void checkCharset() {
		final Script script = this.getScript();
		final String value = "en";
		script.setCharset(value);
		assertEquals(value, script.getCharset());
	}

	@Test
	public void checkDefer() {
		final Script script = this.getScript();
		final String value = "defer?";
		script.setDefer(value);
		assertEquals(value, script.getDefer());
	}

	public void checkSrc() {
		final Script script = this.getScript();
		final String value = "c:/funkytown.js";
		script.setSrc(value);
	}

	@Test
	public void checkType() {
		final Script script = this.getScript();
		final String value = "text/javascript";
		script.setType(value);
		assertEquals(value, script.getType());
	}

	@Test
	public void checkPreservingWhiteSpace() {
		final Script script = this.getScript();
		script.setPreservingWhiteSpace(true);
		assertTrue(script.isPreservingWhiteSpace());
		script.setPreservingWhiteSpace(false);
		assertFalse(script.isPreservingWhiteSpace());
	}

	@Override
	@Test
	public void checkRender() throws IOException {
		final Script script = this.getScript();
		final String body = "function myLeftFoot() {\r\n" +
								"\tvar wittyStatement = \"Goes here\";\r\n" +
							"}";
		script.setScriptBody(body);

		final MockHttpServletRequest req = new MockHttpServletRequest();
		MockHttpServletResponse res = new MockHttpServletResponse();
		script.render(req, res);
		String contentAsString = res.getContentAsString();
		String expectedRender = "<script>//<![CDATA[\r\nfunction myLeftFoot() {\r\n" +
										"\tvar wittyStatement = \"Goes here\";\r\n" +
										"}\r\n" +
										"//]]</script>";
		assertEquals(expectedRender, contentAsString);

		final Script scriptWithSrc = this.getScript().setSrc("/more.js");
		scriptWithSrc.setAutoAppendContext(true);
		req.setContextPath("Testing");
		scriptWithSrc.render(req, res = new MockHttpServletResponse());
		contentAsString = res.getContentAsString();
		expectedRender = "<script src=\"/Testing/more.js\"></script>";
		assertEquals(expectedRender, contentAsString);

	}

	@Test
	public void checkBooleanConstruction() {
		Script s = this.getScript(true);
		assertTrue(s.isAutoAppendContext());

		s = this.getScript(false);
		assertFalse(s.isAutoAppendContext());

		assertNull(s.getScriptBody());

		s.setAutoAppendContext(false);
		assertFalse(s.isAutoAppendContext());
	}

	protected Script getScript() {
		return new Script();
	}

	protected Script getScript(final boolean _autoAppend) {
		return new Script(_autoAppend);
	}

}
